package net.borlis.processor.impl;

import net.borlis.processor.api.object.UserObject;
import net.borlis.processor.api.service.UserService;
import net.borlis.processor.dao.entity.UserEntity;
import net.borlis.processor.dao.repository.UserRepository;
import net.borlis.processor.impl.object.UserObjectImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.math.BigInteger;

/**
 * Created by borlis on 24.12.2014.
 */
@Service
@Qualifier("userServiceImpl")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    public UserObject create(String name, String surname, String company) {
        UserEntity userEntity = new UserEntity(name, surname, company);
        return new UserObjectImpl(userRepository.create(userEntity));
    }

    @Override
    public UserObject read(BigInteger id) {
        return new UserObjectImpl(userRepository.find(id));
    }

    @Override
    public UserObject update(BigInteger id, String name, String surname, String company) {
        UserEntity userEntity = userRepository.find(id);
        userEntity.setCompany(company);
        userEntity.setName(name);
        userEntity.setSurname(surname);
        return new UserObjectImpl(userRepository.update(userEntity));
    }

    @Override
    public void delete(BigInteger id) {
        userRepository.delete(id);
    }

    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }
}
